class UsersController < ApplicationController
  before_action :log_in_user,only: [:edit,:update,:destroy,:index]
  before_action :correct_user,only: [:edit,:update,:add_tag]
  after_action :clear_thread_variable
  def new
   @user= User.new
  end

  def create
    @user=User.new(get_params)
    # debugger
    if @user.save
      # UserMailer.activate_email(@user).deliver
      # 发送邮件
      flash[:success]="please check your mail to activate your account"
      log_in @user
     redirect_to root_url
    # redirect_to :action => 'show',:id =>@user.id
    else
      flash.now[:warning]='注册失败'
      render new_user_path
      render '/users/new'
    end
  end

  def update
    @man =User.find(params[:id])
    if(@man.update_attributes(get_params))
      flash[:success] ="refresh successfully"
      redirect_to @user
    else
      flash.now[:warning]="上传失败"

      render 'edit'
    end
  end

  def choose_blog_types

    redirect_to action: 'show',id: params[:id],tag_name: params[:tag_name]
  end

  def show
    @shadow='profile'

    @user=User.find(params[:id])
    if is_current_user?(@user)
      @qiniu_upload_token=generate_qiniu_upload_token

      @tags=current_user.article_tags.split(",") if current_user.article_tags.present?
    else
      @tags=@user.article_tags.split(",") if @user.article_tags.present?
    end
    if params[:tag_name].present? #点击了文章分类传来的分类参数
       @posts=current_user.posts.tagged(params[:tag_name]).paginate(page: params[:page],per_page: 4)#得到该分类下的微博
    else
       @posts=@user.posts.paginate(:page => params[:page],:per_page => 4)
    end
    @image=current_user.images.first
  end



  def upload_callback
    # @user=User.find(params[:id])
    upload_ret= JSON.parse(Base64.urlsafe_decode64(params[:upload_ret]))
    @img=current_user.images.build(
      title: upload_ret['title'],
      filename: upload_ret['fname'],
      qiniu_hash: upload_ret['hash']
    )
    current_user.update_attribute(:head_pic, upload_ret['hash'])#更新头像
    if @img.save
      flash[:success]='头像上传成功'
      redirect_to :action => 'show',:id =>current_user.id
    else
      flash[:warning]=' 上传失败,请重试'
      redirect_to :action => 'show',:id =>current_user.id
    end
    # render 'users/show'
  end


  def add_article_to_tag
    if params[:select_tag].present?

      tag=current_user.posts.where("id=?",params[:post_num]).first.tags.build(name: params[:select_tag])
      if tag.save
        flash[:success]='添加成功'
      else
        flash[:warning]='添加失败,同一篇文章在一个分类中只能添加一次'
      end
    end
    redirect_to current_user
  end


  def add_tag
    @tag=params[:tags]||params[:types]

    if @tag.present?&&@tag.is_a?(String)
      current_user.tag_list = @tag.split(",")
      current_user.update_attribute(:article_tags,@tag)
    end
    puts "############## + #{current_user.tag_list}"
    # debugger
    redirect_to :action => 'show',:id =>current_user.id #只有correct_user可以访问
  end


  def edit
    @user= User.find(params[:id])


  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success]='删除成功'
    redirect_to users_url
  end

  def index
    @shadow='users'#导航栏标记
    @users = User.paginate(:page => params[:page],:per_page => 10)

    # @mans = User.paginate(page: params[:page])
  end

  def following
    @tittle="我关注的人"
    @user= User.find(params[:id])
    @users=@user.following.paginate(page: params[:page],per_page: 10)
    render "show_follow"
  end

  def followers
    @tittle="关注我的人"
    @user= User.find(params[:id])
    @users=@user.followers.paginate(page: params[:page],per_page: 10)
    render "show_follow"

  end

  def clear_thread_variable
    # Thread.current[:tag_lists]=nil
  end


  private

    def get_params
      params.required(:user).permit(:name,:email,:password,:password_confirmation)
    end
end
